package com.acme.bankapp.domain.account;

import com.acme.bankapp.exceptions.*;

import java.io.Serializable;

public abstract class AbstractAccount implements Account, Serializable {
	private int id;
    private double balance;

	public AbstractAccount(final double amount) throws IllegalArgumentException {
        this.id = getUniqueAccountNumber();
        if (amount < 0) {
            throw new IllegalArgumentException("Balance can not be negative");
        }
        setBalance(amount);
	}

    @Override
    public int getId() {
        return id;
    }

    @Override
    public double getBalance() {
		return balance;
	}

    @Override
    public void setBalance(final double balance){
        this.balance = balance;
    }

    @Override
    public void deposit(final double amount) {
        if (amount < 0) {
            System.out.println("Amount deposit can not be negative");
            return;
        }
        balance += amount;
    }

    @Override
    public long decimalValue()
    {
        return Math.round(balance);
    }

    @Override
    public abstract void withdraw(final double amount) throws NoEnoughFundsException;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AbstractAccount that = (AbstractAccount) o;

        //if (Double.compare(that.getBalance(), this.getBalance()) != 0) return false;
        // Comparison of business keys (id) enough here
        if (this.getId() != that.getId()) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        temp = balance != +0.0d ? Double.doubleToLongBits(balance) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    public static int getUniqueAccountNumber() {
        return (int) System.currentTimeMillis();
    }
}
